﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using BEGDC.CC.Web.UserControls;
using BEGDC.CC.BL.CCFlow.CR;
using BEGDC.CC.Entity;
using BEGDC.CC.BL.Workflow;
using BEGDC.CC.BL.Common;
using BEGDC.Utility.Castle;

namespace BEGDC.CC.Web.SystemManagement
{
	/// <summary>
	/// Summary description for SurveyQuestion.
	/// </summary>
	public class SurveyQuestion : System.Web.UI.Page
	{
		#region VARIABLES
		protected System.Web.UI.WebControls.DropDownList Dp_QuestionType;
		protected System.Web.UI.WebControls.DataGrid DG_SurveyQuestion;
		protected System.Web.UI.HtmlControls.HtmlImage QuestionPaneImg;
		protected System.Web.UI.WebControls.DropDownList ddlFlowType;
		#endregion
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			if (!Page.IsPostBack)
			{
				InitForm();
			}
			else
			{
				this.BindData();
			}
		}

		#region InitForm
		private void InitForm()
		{
			
			//init flow type

			if (((BEGDC.CC.BL.Workflow.CCPrincipal)Page.User).IsInRole(BEGDC.CC.BL.Common.CODOrOCO.BusinessAdmin) && ((BEGDC.CC.BL.Workflow.CCPrincipal)Page.User).IsInRole(BEGDC.CC.BL.Common.CODOrOCO.OCOBusinessAdmin))
			{
				this.ddlFlowType.Items.Add(new System.Web.UI.WebControls.ListItem("Customer Domestic & Overseas Visiting", BEGDC.CC.BL.Workflow.WorkflowTypes.CV));
				this.ddlFlowType.Items.Add(new System.Web.UI.WebControls.ListItem("Customer Reception In Shanghai", BEGDC.CC.BL.Workflow.WorkflowTypes.CR));
				this.ddlFlowType.Items.Add(new System.Web.UI.WebControls.ListItem("Overseas Customer Reception", BEGDC.CC.BL.Workflow.WorkflowTypes.OCR));
				this.ddlFlowType.Items.Add(new System.Web.UI.WebControls.ListItem("Overseas Customer Visiting", BEGDC.CC.BL.Workflow.WorkflowTypes.OCV));
				this.ddlFlowType.Items.Add(new System.Web.UI.WebControls.ListItem("EBD Customer Reception", BEGDC.CC.BL.Workflow.WorkflowTypes.EBD));
				this.ddlFlowType.Items.Add(new System.Web.UI.WebControls.ListItem("EBD Customer Visiting", BEGDC.CC.BL.Workflow.WorkflowTypes.EBDCV));
				this.ddlFlowType.Items.Add(new System.Web.UI.WebControls.ListItem("ESC Customer Reception", BEGDC.CC.BL.Workflow.WorkflowTypes.ESC));
				this.ddlFlowType.Items.Add(new System.Web.UI.WebControls.ListItem("ESC Customer Visiting", BEGDC.CC.BL.Workflow.WorkflowTypes.ESCCV));
			}
			else
			{
				if (((BEGDC.CC.BL.Workflow.CCPrincipal)Page.User).IsInRole(BEGDC.CC.BL.Common.CODOrOCO.BusinessAdmin))
				{
					this.ddlFlowType.Items.Add(new System.Web.UI.WebControls.ListItem("Customer Domestic & Overseas Visiting", BEGDC.CC.BL.Workflow.WorkflowTypes.CV));
					this.ddlFlowType.Items.Add(new System.Web.UI.WebControls.ListItem("Customer Reception In Shanghai", BEGDC.CC.BL.Workflow.WorkflowTypes.CR));

				} 
				else if (((BEGDC.CC.BL.Workflow.CCPrincipal)Page.User).IsInRole(BEGDC.CC.BL.Common.CODOrOCO.OCOBusinessAdmin))
				{
					this.ddlFlowType.Items.Add(new System.Web.UI.WebControls.ListItem("Overseas Customer Reception", BEGDC.CC.BL.Workflow.WorkflowTypes.OCR));
					this.ddlFlowType.Items.Add(new System.Web.UI.WebControls.ListItem("EBD Customer Reception", BEGDC.CC.BL.Workflow.WorkflowTypes.EBD));
					this.ddlFlowType.Items.Add(new System.Web.UI.WebControls.ListItem("ESC Customer Reception", BEGDC.CC.BL.Workflow.WorkflowTypes.ESC));
					//this.ddlFlowType.Items.Add(new System.Web.UI.WebControls.ListItem("Overseas Customer Visiting", BEGDC.CC.BL.Workflow.WorkflowTypes.OCV));

				}
			}
			this.ddlFlowType.SelectedIndex = 0;

			ddlFlowType_SelectedIndexChanged(null,null);

			DataBind();
		}

		private void BindData()
		{
			//INIT QUESTION TABLE
			string QuestionType = this.Dp_QuestionType.SelectedValue;
			ICR CRService = AppContainer.Instance[ServiceConst.CRService] as ICR;
			DataTable QuestionTable = CRService.GetSurveyQuestions(this.ddlFlowType.SelectedValue, QuestionType,false);
			this.DG_SurveyQuestion.DataSource = QuestionTable;
			this.DG_SurveyQuestion.DataBind();

		}

		private void InitQuestionType(string dicType)
		{
			this.Dp_QuestionType.Items.Clear();
			IUtility utility = AppContainer.Instance[ServiceConst.UtilityService] as IUtility;
			IList list = utility.GetAllDicItem(dicType);
			foreach(BEGDC.CC.Entity.DicItem item in list)
			{
				this.Dp_QuestionType.Items.Add(new System.Web.UI.WebControls.ListItem(item.Description, item.DicItemKey)); 
			}

		}
		#endregion

		#region Survey Question Data Bound
		private void QuestionListDateBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
		{
			if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
			{
				System.Web.UI.WebControls.ImageButton Btn_Edit = (System.Web.UI.WebControls.ImageButton) e.Item.Cells[5].FindControl("Btn_Question_Edit");
				string ItemId = e.Item.Cells[0].Text.ToString();
				Btn_Edit.Attributes.Add("onclick", "OpenQuestionPane("+ItemId+");");
			}
		}
		#endregion

		private void QuestionType_SelectChange(object sender, System.EventArgs e)
		{
			string QuestionType = this.Dp_QuestionType.SelectedValue;
			ICR CRService = AppContainer.Instance[ServiceConst.CRService] as ICR;
			DataTable QuestionTable = CRService.GetSurveyQuestions(this.ddlFlowType.SelectedValue, QuestionType,false);
			this.DG_SurveyQuestion.DataSource = QuestionTable;
			this.DG_SurveyQuestion.DataBind();
		}

		private void ddlFlowType_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			string flowtype = ddlFlowType.SelectedValue;
			if (flowtype == BEGDC.CC.BL.Workflow.WorkflowTypes.CR || flowtype == BEGDC.CC.BL.Workflow.WorkflowTypes.CV)
			{
				this.InitQuestionType(BEGDC.CC.BL.Common.DicType.QuestionType);
			}
			else if (flowtype == BEGDC.CC.BL.Workflow.WorkflowTypes.OCV || flowtype == BEGDC.CC.BL.Workflow.WorkflowTypes.OCR ||flowtype == BEGDC.CC.BL.Workflow.WorkflowTypes.ESC || flowtype == BEGDC.CC.BL.Workflow.WorkflowTypes.EBD || flowtype == BEGDC.CC.BL.Workflow.WorkflowTypes.ESCCV || flowtype == BEGDC.CC.BL.Workflow.WorkflowTypes.EBDCV)
			{
				this.InitQuestionType(BEGDC.CC.BL.Common.DicType.OCOQuestionType);
			} 
			
			string QuestionType = this.Dp_QuestionType.SelectedValue;
			ICR CRService = AppContainer.Instance[ServiceConst.CRService] as ICR;
			DataTable QuestionTable = CRService.GetSurveyQuestions(this.ddlFlowType.SelectedValue, QuestionType,false);
			this.DG_SurveyQuestion.DataSource = QuestionTable;
			this.DG_SurveyQuestion.DataBind();
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.ddlFlowType.SelectedIndexChanged += new System.EventHandler(this.ddlFlowType_SelectedIndexChanged);
			this.Dp_QuestionType.SelectedIndexChanged += new System.EventHandler(this.QuestionType_SelectChange);
			this.DG_SurveyQuestion.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.QuestionListDateBound);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion
	}
}
